<template>
  <a-form
    :model="formState"
    ref="formDataRef"
    name="basic"
    :label-col="{ style: 'width:90px' }"
    :wrapper-col="{ style: 'calc(100% - 90px)' }"
    autocomplete="off"
    @finish="onFinish"
    @finishFailed="onFinishFailed"
  >
    <a-form-item label="国家或地区" name="countryRegionManageId" :rules="[{ required: true, message: '请选择国家或地区!' }]">
      <a-select v-model:value="formState.countryRegionManageId" showSearch placeholder="请选择街道">
        <a-select-option v-for="(item, index) in countryOpts" :key="index" :value="item.id">{{ item.region }}</a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item label="城市" name="city" :rules="[{ required: true, message: '请填写城市!' }]">
      <a-input v-model:value="formState.city" :maxlength="50" placeholder="请选择" />
    </a-form-item>
    <a-form-item label="州" name="state" :rules="[{ required: true, message: '请填写州!' }]">
      <a-input v-model:value="formState.state" :maxlength="50" placeholder="请选择" />
    </a-form-item>
    <a-form-item label="邮编" name="postcode" :rules="[{ required: true, message: '请填写邮编!' }]">
      <a-input v-model:value="formState.postcode" :maxlength="50" @keyup="validateZhInputHandle($event, formState, ['postcode'])" placeholder="请选择" />
    </a-form-item>
    <a-form-item label="详细地址" name="address" :rules="[{ required: true, message: '请填写详细地址!' }]">
      <a-input v-model:value="formState.address" :maxlength="100" placeholder="请选择" />
    </a-form-item>
    <a-form-item label="收件人" name="contact" :rules="[{ required: true, message: '请填写收件人!' }]">
      <a-input v-model:value="formState.contact" :maxlength="20" placeholder="请选择" />
    </a-form-item>
    <a-form-item label="电话" name="phone" :rules="[{ required: true, message: '请填写联系电话!' }]">
      <a-input v-model:value="formState.phone" :maxlength="20" @keyup="validateZhInputHandle($event, formState, ['phone'])" placeholder="请输入" />
    </a-form-item>
    <a-form-item label="邮箱" name="email" :rules="[{ required: true, message: '请填写邮箱!' }]">
      <a-input v-model:value="formState.email" :maxlength="20" @keyup="validateZhInputHandle($event, formState, ['email'])" placeholder="请选择" />
    </a-form-item>
    <a-form-item label="公司" name="companyName" :rules="[{ required: true, message: '请填写公司!' }]">
      <a-input v-model:value="formState.companyName" :maxlength="50" placeholder="请输入" />
    </a-form-item>
    <a-form-item :wrapper-col="{ offset: 8, span: 16 }" class="submit-btn-form-item">
      <a-button :loading="loading" type="primary" html-type="submit">确定</a-button>
      <a-button @click="resetHandle">取消</a-button>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
import { toRefs, watch } from "vue";
import InitHook from "./composables/initHook";
import OperationHook from "./composables/operationHook";
const props = withDefaults(
  defineProps<{
    visible: boolean;
    passData: ClientAddressManagement.ListRows;
    type: number;
  }>(),
  {}
);
const emit = defineEmits<{
  (e: "update:visible", blo: boolean): void;
  (e: "submit"): void;
}>();
const { visible, passData, type } = toRefs(props);
const { formDataRef, formState, loading, getCountryOpts, countryOpts } = InitHook();
const { onFinish, onFinishFailed, resetFields, resetHandle, validateZhInputHandle } = OperationHook({
  formDataRef,
  formState,
  passData,
  emit,
  type,
  loading,
  countryOpts
});
const init = () => {
  if (type.value === 2) {
    Object.assign(formState, passData.value);
  }
  getCountryOpts();
};
watch(
  () => visible.value,
  (v: boolean) => {
    if (v) {
      console.log(passData.value);
      init();
    } else {
      resetFields();
      formDataRef.value.clearValidate();
    }
  }
);
init();
</script>
<style lang="stylus" scoped>
:deep(.ant-input-number){
    width: 100%;
}
</style>
